package com.cx.controller.wjr;

import com.cx.pojo.Page;
import com.cx.pojo.PageExample;
import com.cx.pojo.wjr.UserPage;
import com.cx.service.wjr.UserService;
import com.cx.util.wjr.IOxlsl;
import com.cx.vo.ResponseResult;
import com.cx.vo.wjr.Receive;
import com.jptangchina.jxcel.JxcelParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserPageController {
    @Autowired
    UserService userService;
//
    @RequestMapping("addUserPage")
    @ResponseBody
    public UserPage add(Model model, UserPage user){
//        String rdSpeed = null;
//        if(null != user.getRdSpeed()){
//            rdSpeed = user.getRdSpeed().replace(",","");
//            Integer a = Integer.valueOf(rdSpeed);
//            System.out.println(a);
//        }
//        System.out.println(user);
        return user;
    }


    @RequestMapping("userSelectByPhone")
    @ResponseBody
    public List<Page> selectByPhone(Model model, UserPage userpage){
        PageExample pageExample = new PageExample();
        List<Page> list = userService.selectByPagePhone(pageExample);
        return list;
    }

    @RequestMapping("userSelectByNum")
    @ResponseBody
    public List<Page> selectByPageNum(Model model, String pageNums){
        PageExample pageExample = new PageExample();
        pageExample.createCriteria().andPageNumEqualTo(Integer.valueOf(pageNums));
        List<Page> list = userService.selectByPageNum(pageExample);
        return list;
    }

    @RequestMapping("batchAddUserPageAJAX")
    @ResponseBody
    public ResponseResult batchAddAJAX(UserPage userPage,@RequestParam("file") MultipartFile file, ResponseResult responseResult, Model model, UserPage user){

        System.out.println(userPage);
        userPage.setPageNum(123);
        //int count = userService.addUserPage(userPage);
        if (!file.isEmpty()) {
            System.out.println("文件上传成功");
        }else{
            responseResult.setMessage("文件上传失败");
            return responseResult;
        }
        List<Receive> receive = null;
        List<UserPage> userPageList = new ArrayList<>();
        try {
            File sb = IOxlsl.file(file);
            receive = JxcelParser.parser().parseFromFile(Receive.class,sb);
            System.out.println(receive);
            for (Receive r: receive) {
                if(r.isNull()){
                    UserPage userPageAdd = new UserPage();
                    userPageAdd.setSendName(userPage.getSendName());
                    String userReceivePhone = r.getReceivePhone().replace(",","");
                    System.out.println(userReceivePhone);
                    userPageAdd.setSendPhone(user.getSendPhone());

                    userPageAdd.setSendAddress(user.getSendAddress());
                    userPageAdd.setSendArea(user.getSendArea());
                    userPageAdd.setPageType(r.getPageType());
                    userPageAdd.setReceivePhone(userReceivePhone);
                    userPageAdd.setReceiveAddress(r.getReceiveAddress());
                    userPageAdd.setReceiveName(r.getReceiveName());
                    userPageAdd.setWeight(r.getWeight());
                    userPageList.add(userPageAdd);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setCode(0);
            responseResult.setMessage("文件格式不符合要求");
            return responseResult;
        }
        System.out.println(userPageList);
        responseResult.setCode(1);
        responseResult.setData(userPageList);
        responseResult.setMessage("转换成功");
        return responseResult;
    }

    @RequestMapping("batchAddUserPage")
    @ResponseBody
    public ResponseResult batchAdd(UserPage userPage,@RequestParam("file") MultipartFile file, ResponseResult responseResult, Model model, UserPage user){
        String rdSpeed = null;
        System.out.println(user);


        if (!file.isEmpty()) {
            System.out.println("文件上传成功");
        }else{
            responseResult.setMessage("文件上传失败");
            return responseResult;
        }
        List<Receive> receive = null;
        List<UserPage> userPageList = new ArrayList<>();
        try {
            File sb = IOxlsl.file(file);
            receive = JxcelParser.parser().parseFromFile(Receive.class,sb);
            for (Receive r: receive) {
                if(r.isNull()){
                    UserPage userPageAdd = new UserPage();
                    int pageNum = (int) (System.currentTimeMillis()%10000);
                    int pageCode = (int) (System.currentTimeMillis()%1000000);
                    userPageAdd.setPageNum(pageNum);
                    userPageAdd.setPageCode(String.valueOf(pageCode));
                    userPageAdd.setStatus("已下单");
                    String userReceivePhone = r.getReceivePhone().replace(",","");
                    userPageAdd.setSendName(userPage.getSendName());
                    userPageAdd.setSendPhone(user.getSendPhone());
                    userPageAdd.setSendAddress(user.getSendAddress());
                    userPageAdd.setSendArea(user.getSendArea());
                    userPageAdd.setPageType(r.getPageType());
                    userPageAdd.setReceivePhone(userReceivePhone);
                    userPageAdd.setReceiveAddress(r.getReceiveAddress());
                    userPageAdd.setReceiveName(r.getReceiveName());
                    userPageAdd.setWeight(r.getWeight());
                    userPageList.add(userPageAdd);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setCode(0);
            responseResult.setMessage("文件格式不符合要求");
            return responseResult;
        }

        System.out.println(userPageList);


        for (UserPage u:userPageList) {
            int count = userService.addUserPage(u);
        }

        responseResult.setCode(1);
        responseResult.setData(userPageList);
        responseResult.setMessage("添加成功");

        return responseResult;
    }
}
